package com.example.demo.practice;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xujimou
 * @version 2.0
 * @Description
 * @date 2022/2/17 15:43
 */
public class test_大数 {


    static class Node{
        int val;
        int num; //表示几位
        Node next;
        public Node(int val,int num) {
            this.val = val;
            this.num=num;
        }
    }




    public static String multic(String a,String b){


        char[] aa = new StringBuffer(a).reverse().toString().toCharArray();
        char[] bb = new StringBuffer(b).reverse().toString().toCharArray();

        Node headA = new Node((int)aa[0]-'0',0);
        Node headB = new Node((int)bb[0]-'0',0);

        Node startA = headA;
        Node stratB = headB;

        for(int i =1;i<aa.length;i++){
            headA.next = new Node(aa[i]-'0',i);
            headA = headA.next;
        }

        for(int i =1;i<bb.length;i++){
            headB.next = new Node(bb[i]-'0',i);
            headB = headB.next;
        }

        List<Node> result = new ArrayList<>();

        while(startA != null){
            Node tempB = stratB;
            while(tempB != null){
                Node node = new Node(startA.val * tempB.val,startA.num + tempB.num);
                result.add(node);
                tempB = tempB.next;
            }
            startA = startA.next;
        }

        List<String> list  = new ArrayList<>();
        for(Node node : result){
            String str = node.val + "";
            for(int i =0 ;i<node.num;i++){
                str = str +"0";
            }
            list.add(str);
        }


        String res = "0";
        for(String temp : list){
            res = bigAdd(res,temp);
        }
        return res;
    }

    // 1 2 3 + 1 2 3
    // 3 2 1 + 3 2 1
    // 6 4 2

    public static String bigAdd(String a,String b){


        char[] aa = new StringBuffer(a).reverse().toString().toCharArray();
        char[] bb = new StringBuffer(b).reverse().toString().toCharArray();

        Node head1 = new Node((int)aa[0]-'0',0);
        Node head2 = new Node((int)bb[0]-'0',0);

        Node start1 = head1;
        Node start2 = head2;

        for(int i =1;i<aa.length;i++){
            head1.next = new Node(aa[i]-'0',i);
            head1 = head1.next;
        }

        for(int i =1;i<bb.length;i++){
            head2.next = new Node(bb[i]-'0',i);
            head2 = head2.next;
        }

        String s = bigAdd(start1, start2, "", 0);

        return new StringBuffer(s).reverse().toString();
    }


    public static String bigAdd(Node head1,Node head2,String result,int other){

        if(head1 !=null && head2!=null){
            int res = head1.val + head2.val + other;
            if(res < 10){
                return bigAdd(head1.next,head2.next,result+res,0);
            }else{
                other = res / 10;
                res = res % 10;
                return bigAdd(head1.next,head2.next,result+res,other);
            }

        }else if(head1 !=null){
            int res = head1.val + 0 + other;
            if(res < 10){
                return bigAdd(head1.next,null,result+res,0);
            }else{
                other = res / 10;
                res = res % 10;
                return bigAdd(head1.next,null,result+res,other);
            }
        }else if (head2 != null){
            int res = head2.val + 0 + other;
            if(res < 10){
                return bigAdd(null,head2.next,result+res,0);
            }else{
                other = res / 10;
                res = res % 10;
                return bigAdd(null,head2.next,result+res,other);
            }

        }else if(other >0){
            return result+other;
        }
        return result;
    }




    public static void main(String[] args) {

        System.out.println(multic("22","10"));

        bigAdd("12","90");
    }

}
